<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
 
	<head>
		
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		
		<title>Simpla Admin | Sign In</title>
		
		<!--                       CSS                       -->
	  
		<!-- Reset Stylesheet -->
		<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
	  
		<!-- Main Stylesheet -->
		<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen" />
		
		<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
		<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />	
		
		<!-- Colour Schemes
	  
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
	 
		-->
		
		<!-- Internet Explorer Fixes Stylesheet -->
		
		<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
		
		<!--                       Javascripts                       -->
	  
		<!-- jQuery -->
		<script type="text/javascript" src="resources/scripts/jquery-1.3.2.min.js"></script>
		
		<!-- jQuery Configuration -->
		<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
		
		<!-- Facebox jQuery Plugin -->
		<script type="text/javascript" src="resources/scripts/facebox.js"></script>
		
		<!-- jQuery WYSIWYG Plugin -->
		<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
		
		<!-- Internet Explorer .png-fix -->
		
		<!--[if IE 6]>
			<script type="text/javascript" src="resources/scripts/DD_belatedPNG_0.0.7a.js"></script>
			<script type="text/javascript">
				DD_belatedPNG.fix('.png_bg, img, li');
			</script>
		<![endif]-->
		
	</head>
  
	<body id="login">
		
		<div id="login-wrapper" class="png_bg">
			<div id="login-top">
			
				<h1>Simpla Admin</h1>
				<!-- Logo (221px width) -->
				<img id="logo" src="resources/images/logo.png" alt="Simpla Admin logo" />
			</div> <!-- End #logn-top -->
			
			<div id="login-content">
<?php
session_start();

include 'conn.php';

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    { 

        $login_user = $Mysqli->real_escape_string($_POST['gebruikersnaam']); 
        $login_pass = $Mysqli->real_escape_string(sha1($_POST['wachtwoord']));
        $user_ip = $_SERVER['REMOTE_ADDR'];
        srand ((double) microtime( )*1000000);
        $session_id = rand(1000,1000000);
 
        $q1 = "
        SELECT 
            id,
            username,
            block
        FROM 
            users
        WHERE
            username = '".$login_user."'  
        AND
            password = '".$login_pass."'
        ";

            if(!$r1 = $Mysqli->query($q1))
        {
        
            echo '<div class="notification error png_bg"><div>Er is een fout opgetreden!. '. $Mysqli->error;
			echo '</div></div>';
        
        }
                                        
                                
                elseif($Mysqli->affected_rows == 1)
                {
    

                                while ($row = $r1->fetch_assoc ())
                                { 
                                
                                        $_SESSION["username"] = $row['username']; 
                                        $_SESSION['user_id'] = $row['id'];   
                                        $_SESSION['user_ip'] = $user_ip;
                                        $_SESSION['session_id'] = $session_id;
                                               
                                    if ($row['block'] == 1)
                                    {
                                        echo '<div class="notification error png_bg"><div>Je bent geblokkeerd, je kunt niet meer inloggen!';
										echo '</div></div>';
                                    } 
                                        else 
                                        { 
                                            //Inloggen gelukt!!
                                            header("location: " . 'index.php?msg=succes');
                                        }

                                
    
    
                    $q2 = "
                    INSERT INTO
                        sessions
                    (
                        user_id,
                        session_id,
                        user_ip
                    )
                    VALUES
                    (
                        '".$row['id']."',
                        '".$session_id."',
                        '".$user_ip."'
                    )
                    ";
                                }

                                    if (!$Mysqli->query ($q2) )
                                    {
                                    
                                        echo '<div class="notification error png_bg"><div>Er is een fout opgetreden!'. $Mysqli->error;
										echo '</div></div>';
                                    }
                                



                }
                    else 
                    {   
                        echo '<div class="notification error png_bg"><div>Gebruikersnaam of wachtwoord onjuist, probeer het opnieuw!</div></div>'; 
						?>
							<form method='post'>  
								<p>		
									<label>Naam:</label>
									<input class="text-input" type='text' name='gebruikersnaam'>
								</p>
								<div class="clear"></div>
								<p>
									<label>Wachtwoord:</label> 
									<input class="text-input" type='password' name='wachtwoord'>  
								</p>
								<div class="clear"></div>
						
								<div class="clear"></div>
								<p>		
									<input class="button" type="submit" value="Login" />
								</p>
							
                    </form>
					<?php
                    }  
          
    } 
        else 
        {
            ?>
                
				
                <?php 
                    if(isset($_GET['error']))
                    {
                        if($_GET['error'] == 'sess')
                        {
                            echo '<div class="notification error png_bg"><div>De sessie is ongeldig! Log aub opnieuw in!</div></div>';
                        }
                    } 
                ?>
                    <form method='post'>  
						<p>		
							<label>Naam:</label>
							<input class="text-input" type='text' name='gebruikersnaam'>
                        </p>
						<div class="clear"></div>
						<p>
							<label>Wachtwoord:</label> 
							<input class="text-input" type='password' name='wachtwoord'>  
						</p>
                        <div class="clear"></div>
						
						<div class="clear"></div>
						<p>		
                                <input class="button" type="submit" value="Login" />
                        </p>
							
                    </form>         
        <?php
        }  
        ?>
</div> <!-- End #login-content -->
			
		</div> <!-- End #login-wrapper -->
		
  </body>
  
</html>